<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Global functions for article...
 * used from article->article article->categorys
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */

/**
 * Sort and create category-entries for Table.
 * Maximum 4 Subcategorys!
 * Only the submenu-capable categories are given back
 * to the category editor for ($all = false) 
 * @param  string $refDb     Username
 * @param  string $data Userpassword
 * @param  boolean $all If True return all Subs otherwise only 3 Subs
 * @return mixed  returns true if Login ok
 */
function sortCategorys($refDb, $data, $all = true)
{
	$buffer = array();

	foreach ($data as $sub1)
	{
		$buffer[] = $sub1;
		if ($resultSub1 = $refDb->get_results("SELECT category_id, category_name, category_active, " .
			"category_order, category_top FROM category" .
			" WHERE category_top = '" . $sub1->category_id . "'" .
			" ORDER BY category_order"))
		{
			foreach ($resultSub1 as $sub2)
			{
				$sub2->category_name = '&emsp;&rarr; ' . $sub2->category_name;
				$buffer[] = $sub2;

				if ($resultSub2 = $refDb->get_results("SELECT category_id, category_name, " .
					"category_active, category_order, " .
					"category_top FROM category" .
					" WHERE category_top = '" .
					$sub2->category_id . "'" .
					" ORDER BY category_order"))
				{
					foreach ($resultSub2 as $sub3)
					{
						$sub3->category_name = '&emsp;&emsp;&rarr; ' . $sub3->category_name;
						$buffer[] = $sub3;

						if ($resultSub3 = $refDb->get_results("SELECT category_id, category_name, " .
							"category_active, category_order, " .
							"category_top FROM category" .
							" WHERE category_top = '" .
							$sub3->category_id . "'" .
							" ORDER BY category_order"))
						{

							foreach ($resultSub3 as $sub4)
							{
								$sub4->category_name = '&emsp;&emsp;&emsp;&rarr; ' . $sub4->category_name;
								$buffer[] = $sub4;

								if ($all)
								{
									if ($resultSub4 = $refDb->get_results("SELECT category_id, category_name, " .
										"category_active, category_order, " .
										"category_top FROM category" .
										" WHERE category_top = '" .
										$sub4->category_id . "'" .
										" ORDER BY category_order"))
									{

										foreach ($resultSub4 as $sub5)
										{
											$sub5->category_name = '&emsp;&emsp;&emsp;&emsp;&rarr; ' .
												$sub5->category_name;
											$buffer[] = $sub5;
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return $buffer;
}

/**
 * Read names of browsepages for article
 *
 * @return list of files as array, without extension
 */
function getBrowsepages($art = 'article')
{
	$files = array();
	if ($handle = opendir($_SERVER["DOCUMENT_ROOT"] . '/template/' . $art . '.browse'))
	{
		while (false !== ( $file = readdir($handle) ))
		{
			if ($file != "." && $file != "..")
			{
				$files[] = str_replace('.tpl.php', '', $file);
			}
		}
		closedir($handle);
		return $files;
	}
}

?>
